//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.reminder;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.*;
import org.jpgolay.gui.date.DateComboBox;
import org.jpgolay.gui.fixed.TableViewer;
import org.jpgolay.me.util.*;

// Referenced classes of package org.jpgolay.me.reminder:
//            ReminderCRUD, ReminderWPGUI, Reminder
public class ReminderWPController {

    public class LabelCellRenderer extends DefaultTableCellRenderer {

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
            Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
            label = (JLabel) comp;
            label.setForeground(Color.WHITE);
            return label;
        }
        protected JLabel label;

        public LabelCellRenderer() {
        }
    }

    public ReminderWPController(ReminderWPGUI reminderWPGUI, Parameter param, JDesktopPane desktop) {
        this.reminderWPGUI = null;
        this.reminderWPGUI = reminderWPGUI;
        this.param = param;
        this.desktop = desktop;
        date = new MEDate();
        reminderCrud = new ReminderCRUD();
        labelRenderer = new LabelCellRenderer();
        initReminderWPTable();
    }

    private void initReminderWPTable() {
        reminderWPGUI.getRefreschB().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionSearch();
            }
        });
        reminderWPGUI.getTableViewerPanel().setTypes(new Class[]{
                    java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Boolean.class
                });
        reminderWPGUI.getTableViewerPanel().setLastColumnNotEditable(1);
        reminderWPGUI.getTableViewerPanel().setCellEditable(true);
        reminderWPGUI.getTableViewerPanel().setAlternateColor(MEUtility.REMINDER_LINE);
        reminderWPGUI.getTableViewerPanel().setPreferredColumnSize(new int[]{
                    1, 100, 100, 200, 10
                });
        reminderWPGUI.getTableViewerPanel().setColumnNames(new String[]{
                    "", "(PIECE)", "DATE", "NOTES", "?"
                });
        Object data[][] = new Object[10][5];
        for (int i = 0; i < data.length; i++) {
            data[i][0] = null;
            data[i][1] = null;
            data[i][2] = null;
            data[i][3] = null;
            data[i][4] = new Boolean(false);
        }

        reminderWPGUI.getTableViewerPanel().setData(data);
        actionSearch();
    }

    private void setDateColumn(JTable table, TableColumn dateColumn) {
        DateComboBox comboBox = new DateComboBox();
        dateColumn.setCellEditor(new DefaultCellEditor(comboBox));
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setToolTipText("Cliquer pour s\351l\351ctionner la date");
        dateColumn.setCellRenderer(renderer);
    }

    private void actionUpdateReminderRow(int row) {
        Reminder reminder = new Reminder();
        reminder.setId((Integer) reminderWPGUI.getTableViewerPanel().getTable().getValueAt(row, 0));
        reminder.setDate((String) reminderWPGUI.getTableViewerPanel().getTable().getValueAt(row, 2));
        reminder.setNotes((String) reminderWPGUI.getTableViewerPanel().getTable().getValueAt(row, 3));
        Boolean b = (Boolean) reminderWPGUI.getTableViewerPanel().getTable().getValueAt(row, 4);
        if (b.booleanValue()) {
            reminder.setStatus(new Integer(1));
        } else {
            reminder.setStatus(new Integer(0));
        }
        reminder.setUserId(param.getUserId());
        if (reminder.getId() == null) {
            Integer pk = reminderCrud.insertReminderRow(reminder);
            reminderWPGUI.getTableViewerPanel().getTable().setValueAt(pk, row, 0);
        } else if (!reminderCrud.updateReminderRow(reminder)) {
            JOptionPane.showMessageDialog(null, "Impossible d'enregistrer les donn\351es !");
        }
    }

    private void actionSearch() {
        Object data[][] = reminderCrud.getDataList("SELECT id,piece_id,date,notes,status FROM reminder WHERE status=0 AND date <='" + date.getMysql() + "'");
        if (data != null) {
            Object dataShow[][] = new Object[data.length + 10][5];
            for (int i = 0; i < data.length; i++) {
                dataShow[i][0] = new Integer((String) data[i][0]);
                dataShow[i][1] = data[i][1];
                dataShow[i][2] = date.toME((String) data[i][2]);
                dataShow[i][3] = data[i][3];
                dataShow[i][4] = new Boolean(false);
            }

            for (int i = data.length; i < data.length + 10; i++) {
                dataShow[i][0] = null;
                dataShow[i][1] = null;
                dataShow[i][2] = null;
                dataShow[i][3] = null;
                dataShow[i][4] = new Boolean(false);
            }

            reminderWPGUI.getTableViewerPanel().setData(dataShow);
            TableColumnModel model = reminderWPGUI.getTableViewerPanel().getTable().getColumnModel();
            model = reminderWPGUI.getTableViewerPanel().getTable().getColumnModel();
            model.getColumn(0).setCellRenderer(labelRenderer);
            setDateColumn(reminderWPGUI.getTableViewerPanel().getTable(), reminderWPGUI.getTableViewerPanel().getTable().getColumnModel().getColumn(2));
            reminderWPGUI.getTableViewerPanel().getTableModel().addTableModelListener(new TableModelListener() {
                public void tableChanged(TableModelEvent e) {
                    actionUpdateReminderRow(e.getFirstRow());
                }
            });
        }
    }
    private ReminderWPGUI reminderWPGUI;
    private JDesktopPane desktop;
    private Parameter param;
    private MEDate date;
    private ReminderCRUD reminderCrud;
    private LabelCellRenderer labelRenderer;
}
